//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GuidanceExplorer.Controls
{
    public partial class SpellingCheckerDialog : Form
    {
        Dictionary<string, List<string>> _suggestionsDictionary;
        List<string> _errors;
        int index;
        public event EventHandler<NextSpellingErrorEventArgs> NextSpellingError;
        public event EventHandler<ReplaceErrorEventArgs> ReplaceError;

        public SpellingCheckerDialog(Dictionary<string, List<string>> suggestionsDictionary, List<string> errors)
        {
            InitializeComponent();

            index = 0;
            _errors = errors;
            _suggestionsDictionary = suggestionsDictionary;
        }

        private void lstSuggestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtReplace.Text = lstSuggestions.SelectedItem.ToString();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (index < _errors.Count - 1)
            {
                index++;
                string error = _errors[index];
                txtError.Text = error;
                txtReplace.Text = string.Empty;
                lstSuggestions.DataSource = _suggestionsDictionary[error];
                NextSpellingError(this, new NextSpellingErrorEventArgs(error));
            }
            else
            {
                this.Close();
            }
        }

        private void SpellingCheckerDialog_Load(object sender, EventArgs e)
        {
            if (_errors.Count > 0)
            {
                txtError.Text = _errors[0];
                lstSuggestions.DataSource = _suggestionsDictionary[_errors[0]];
                NextSpellingError(this, new NextSpellingErrorEventArgs(_errors[0]));
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (txtReplace.Text != null && txtReplace.Text.Length > 0)
            {
                ReplaceError(this, new ReplaceErrorEventArgs(txtError.Text, txtReplace.Text));
                btnNext_Click(null, null);
            }
            else
            {
                MessageBox.Show("Please, insert the text to replace the spelling error", "Spell Checker", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }


}
